/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cart.hibernate.paymentMethod;

/**
 *
 * @author BibooChung
 */
import cart.hibernate.HibernateUtil;
import java.util.List; 
import java.util.Iterator; 
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManagePaymentMethod {
  /*  public static void main(String[] args) {
        try{
           factory = new Configuration().configure("cart/hibernate/hibernate.cfg.xml").buildSessionFactory();
        }catch (Throwable ex) { 
           System.err.println("Failed to create sessionFactory object." + ex);
           throw new ExceptionInInitializerError(ex); 
        }
        ManagePaymentMethod manager = new ManagePaymentMethod();
        manager.truncatePaymentMethod();

        long empID1 = manager.addPaymentMethod("Credit Card - Master");
        long empID2 = manager.addPaymentMethod("Credit Card - VISA");
        long empID3 = manager.addPaymentMethod("PPS");
        long empID4 = manager.addPaymentMethod("WOHO");
      	
    }
    
    
    public long resetAutoIncrement(Session session) {
        String query = "ALTER TABLE payment_method AUTO_INCREMENT = 1";
    	long updateState = session.createSQLQuery(query).executeUpdate();
    	
    	return updateState;
    }

    public void truncatePaymentMethod(){
    	Session session = HibernateUtil.getSessionFactory().openSession();
    	Transaction tx = null;
    	try{
    	   tx = session.beginTransaction();
    	   List payMethodList = session.createQuery("FROM PaymentMethod").list(); 
    	   for (Iterator iterator = payMethodList.iterator(); iterator.hasNext();){
		PaymentMethod payMethod = (PaymentMethod) iterator.next(); 
		session.delete(payMethod); 
    	   }
           resetAutoIncrement(session);
    	   tx.commit();
    	}catch (HibernateException e) {
    	   if (tx!=null) tx.rollback();
    	   e.printStackTrace(); 
    	}finally {
    	   session.close(); 
    	}
    }
    */
    public List getAllPaymentMethod() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List allPaymentMethod = null;
        try{
           tx = session.beginTransaction();
           allPaymentMethod = session.createQuery("FROM PaymentMethod").list(); 
           tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
        return allPaymentMethod;
    }
    /*
    public void listPaymentMethod() {
    	Session session = HibernateUtil.getSessionFactory().openSession();
    	Transaction tx = null;
    	try{
    	   tx = session.beginTransaction();
    	   List payMethodList = session.createQuery("FROM PaymentMethod").list(); 
    	   for (Iterator iterator = payMethodList.iterator(); iterator.hasNext();){
    	      PaymentMethod payMethod = (PaymentMethod) iterator.next(); 
    	      System.out.print("id: " + payMethod.getPaymentMethodId()); 
    	      System.out.println("  method: " + payMethod.getPaymentMethod()); 
    	   }
    	   tx.commit();
    	}catch (HibernateException e) {
    	   if (tx!=null) tx.rollback();
    	   e.printStackTrace(); 
    	}finally {
    	   session.close(); 
    	}
    }
    */
    public long addPaymentMethod(String methodName) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        long paymentMethodId = -1;

        try {
            tx = session.beginTransaction();
            PaymentMethod payMethod = new PaymentMethod(methodName);
            paymentMethodId = (Long) session.save(payMethod);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return paymentMethodId;
    }
    
    public void updatePaymentMethod(long payMethodId, String newMethod) {
        Session session = HibernateUtil.getSessionFactory().openSession();
    	Transaction tx = null;
    	try{
    	   tx = session.beginTransaction();
    	   PaymentMethod payMethod = (PaymentMethod)session.get(PaymentMethod.class, payMethodId); 
    	   payMethod.setPaymentMethod(newMethod);
           session.update(payMethod);
    	   tx.commit();
    	}catch (HibernateException e) {
    	   if (tx!=null) tx.rollback();
    	   e.printStackTrace(); 
    	}finally {
    	   session.close(); 
    	}
    }

    public void deletePaymentMethod(long payMethodId) {
    	Session session = HibernateUtil.getSessionFactory().openSession();
    	Transaction tx = null;
    	try{
    	   tx = session.beginTransaction();
    	   PaymentMethod payMethod = (PaymentMethod)session.get(PaymentMethod.class, payMethodId); 
    	   session.delete(payMethod); 
    	   tx.commit();
    	}catch (HibernateException e) {
    	   if (tx!=null) tx.rollback();
    	   e.printStackTrace(); 
    	}finally {
    	   session.close(); 
    	}
    }
}




























